let musiclist = []
let nowPlayingIndex = 0
// 获取全局唯一的背景音频管理器
const backgroundAudioManager = wx.getBackgroundAudioManager()
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 歌曲封面图片
    picUrl:'',
    isPlaying:false,
    isLyricShow:false,
    lyric:'',
    isSame:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options);
    musiclist = wx.getStorageSync('musiclist')
    nowPlayingIndex = options.index
    this.loadMusicDetail(options.musicId)
    // this.bindBGMEvent()
  },

  timeupdata(event){
    // console.log(event);
    this.selectComponent(".lyric").updata(event.detail.currentTime)
  },
  musicPlay(){
    this.setData({
      isPlaying:true
    })
  },
  musicPause(){
    this.setData({
      isPlaying:false
    })
  },
  loadMusicDetail(musicId){
    if(musicId == app.getPlayMusicId()){
      this.setData({
        isSame:true
      })
    }else{
      this.setData({
        isSame:false
      })
    }
    if(!this.data.isSame){
      backgroundAudioManager.stop()
    }
    
    this.setData({
      isPlaying:false
    })
    const music = musiclist[nowPlayingIndex]
    // console.log(music);
    wx.setNavigationBarTitle({
      title: music.name,
    })
    this.setData({
      picUrl:music.al.picUrl
    })
    app.setPlayMusicId(musicId)
    wx.showLoading({
      title: '歌曲加载中',
    })
    wx.cloud.callFunction({
      name:"music",
      data:{
        musicId,
        $url:'musicUrl'
      }
    }).then(res => {
      let result = res.result
      // console.log(result);
      if(result.data[0].url == null){
        wx.showToast({
          title: '无权限播放',
          icon:'error'
        })
        setTimeout(() => {
          this.onNext()
        }, 1000);
        return
      }
      if(!this.data.isSame){
        backgroundAudioManager.src = result.data[0].url
        // 歌曲名
        backgroundAudioManager.title = music.name
        // 歌手名
        backgroundAudioManager.singer = music.ar[0].name
        // 播放图片
        backgroundAudioManager.coverImgUrl = music.al.picUrl
        // 专辑名称
        backgroundAudioManager.epname = music.al.name

        this.savePlayHistory()
      }
      
      this.setData({
        isPlaying:true
      })
      wx.hideLoading()

      // 加载歌词
      wx.cloud.callFunction({
        name:'music',
        data:{
          musicId,
          $url:'lyric'
        }
      }).then(res => {
        console.log(res);
        let lyric = '暂无歌词'
        const lrc = res.result.lrc
        if(lrc){
          lyric = lrc.lyric
        }
        this.setData({
          lyric
        })
      })
    })
  },

  savePlayHistory(){
    let music = musiclist[nowPlayingIndex]
    const openid = app.globalData.openid
    console.log(music);
    const history = wx.getStorageSync(openid)
    let bHave = false
    for(let i = 0;i< history.length;i++){
      if(history[i].id == music.id){
        bHave = true
        break
      }
    }
    if(!bHave){
      history.unshift(music)
      wx.setStorageSync(openid, history)
    }
    
  },

  // bindBGMEvent(){
  //   backgroundAudioManager.onPlay(() => {
  //     this.setData({
  //       isPlaying:true
  //     })
  //   })
  // },
  // 点击播放与暂停
  changeStatus(){
    if(this.data.isPlaying){
      backgroundAudioManager.pause()
    }else{
      backgroundAudioManager.play()
    }
    this.setData({
      isPlaying:!this.data.isPlaying
    })
  },
  // 点击切换上一首
  onPrev(){
    nowPlayingIndex--
    if(nowPlayingIndex < 0){
      nowPlayingIndex = musiclist.length - 1
    }
    this.loadMusicDetail(musiclist[nowPlayingIndex].id)
  },
  // 点击切换下一首
  onNext(){
    nowPlayingIndex++
    if(nowPlayingIndex === musiclist.length){
      nowPlayingIndex = 0
    }
    this.loadMusicDetail(musiclist[nowPlayingIndex].id)
  },

  changeLyricShow(){
    this.setData({
      isLyricShow:!this.data.isLyricShow
    })
  }
  
})